package com.huawei.register.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fengwenyi.api_result.helper.ResultHelper;
import com.fengwenyi.api_result.model.ResultModel;
import com.huawei.register.pojo.UserInfo;
import com.huawei.register.service.UserInfoService;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping(value = "register")
@CrossOrigin
public class RegisterController {
    @Autowired
    private UserInfoService userInfoService;


    @GetMapping("/select")
    public String selectByName() {
        List<UserInfo> list = userInfoService.getUserInfo("强枭峰");
        return list.toString();
    }


    @PostMapping("/addUser")
    public String addUser(String userName, String idCard) {
        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(idCard)) {
            return "用户名或者密码为空";
        }

        UserInfo userInfo = new UserInfo();
        userInfo.setUserName(userName);
        userInfo.setUserIdCard(idCard);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        userInfo.setRegisterDate(simpleDateFormat.format(new Date()));

        if (userInfoService.addUser(userInfo)) {
            return "插入成功！";
        }

        return "插入失败！";
    }


    @GetMapping("/selectUserByPage/{currentPage}")
    public ResultModel selectUserByPage(@PathVariable("currentPage") int currentPage) {
        IPage<UserInfo> userInfoPage = userInfoService.selectUserByPage(currentPage);

        return ResultHelper.success("success", userInfoPage);
    }


    @GetMapping("/exportUserWithExcel")
    public void exportUserWithExcel(HttpServletResponse response) throws IOException {
        //表头数据
        String[] header = {"ID", "姓名", "身份证号", "注册时间"};

        //声明一个工作簿
        HSSFWorkbook workbook = new HSSFWorkbook();

        //生成一个表格，设置表格名称为"学生表"
        HSSFSheet sheet = workbook.createSheet("用户注册信息表");

        //设置表格列宽度为10个字节
        sheet.setDefaultColumnWidth(10);

        //创建第一行表头
        HSSFRow headrow = sheet.createRow(0);

        //遍历添加表头(下面模拟遍历学生，也是同样的操作过程)
        for (int i = 0; i < header.length; i++) {
            //创建一个单元格
            HSSFCell cell = headrow.createCell(i);

            //创建一个内容对象
            HSSFRichTextString text = new HSSFRichTextString(header[i]);

            //将内容对象的文字内容写入到单元格中
            cell.setCellValue(text);
        }

        //模拟遍历结果集，把内容加入表格
        List<UserInfo> allUsers = userInfoService.selectAllUserInfo();
        int userSize = allUsers.size();
        for (int i = 0; i < userSize; i++) {
            HSSFRow row = sheet.createRow(i + 1);
            UserInfo userInfo = allUsers.get(i);

            // 创建单元格
            HSSFCell cell00 = row.createCell(0);
            HSSFCell cell01 = row.createCell(1);
            HSSFCell cell02 = row.createCell(2);
            HSSFCell cell03 = row.createCell(3);

            HSSFRichTextString userId = new HSSFRichTextString(String.valueOf(userInfo.getUserId()));
            HSSFRichTextString userName = new HSSFRichTextString(userInfo.getUserName());
            HSSFRichTextString userIdCard = new HSSFRichTextString(userInfo.getUserIdCard());
            HSSFRichTextString registerDate = new HSSFRichTextString(userInfo.getRegisterDate());
            cell00.setCellValue(userId);
            cell01.setCellValue(userName);
            cell02.setCellValue(userIdCard);
            cell03.setCellValue(registerDate);
        }

        //准备将Excel的输出流通过response输出到页面下载
        //八进制输出流
        response.setContentType("application/octet-stream");

        //这后面可以设置导出Excel的名称，此例中名为student.xls
        response.setHeader("Content-disposition", "attachment;filename=user_register.xls");

        //刷新缓冲
        response.flushBuffer();

        //workbook将Excel写入到response的输出流中，供页面下载
        workbook.write(response.getOutputStream());
    }
}
